import api from "../api/request";

export interface UserProfilePayload {
  nickname: string;
  gender: string;
  birthday: string;
  bio: string;
}

export async function submitUserProfileApi(payload: UserProfilePayload) {
  // 假设 first-log 仅用于首次登录信息补全，保留
  return api.post("/api/profile/first-log", payload);
}

export interface RegisterPayload {
  email: string;
  password: string;
  code: string;
}

export async function registerUserApi(payload: RegisterPayload) {
  // 按后端文档接口调整，支持 application/json
  return api.post("/users/register", payload);
}

//发送验证码接口
export async function sendCodeApi(email: string) {
  return api.post("/users/code", { email });
}

//修改密码接口
export async function updatePasswordApi(
  email: string,
  code: string,
  newPassword: string
) {
  return api.post("/users/updatePassword", { email, code, newPassword });
}

//修改用户基本信息
export async function updateCommonDataApi(
  id: number,
  nickname: string,
  gender: string,
  personalIntroduce: string,
  birthday: string,
  LuojiaAccount: string,
  email: string
) {
  return api.post("/users/updateCommonData", {
    id,
    nickname,
    gender,
    personalIntroduce,
    birthday,
    LuojiaAccount,
    email,
  });
}

//忘记密码重置密码的接口
export async function resetPasswordApi(email: string, code: string) {
  return api.post("/users/resetPassword", { email, code });
}

//用户密码登陆接口
export async function loginByPasswordApi(email: string, password: string) {
  return api.post("/users/loginByPassword", { email, password });
}

//用户验证码登录
export async function loginByCodeApi(email: string, code: string) {
  return api.post("/users/loginByCode", { email, code });
}

export async function loginUserApi(email: string, password: string) {
  return api.post("/api/login", { email, password });
}

/**
 * 更新用户头像
 * @param id 用户id
 * @param file 头像文件（File对象）
 * @returns Promise<ResultObject>
 */
export async function updateAvatarApi(id: number | string, file: File) {
  const formData = new FormData();
  formData.append("id", id.toString());
  formData.append("file", file);
  return api.post("/users/updateAvatar", formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });
}

/**
 * 根据ID查询用户信息
 * @param id 用户ID
 * @returns Promise<ResultObject>
 */
export async function getUserByIdApi(id: number | string) {
  return api.post("/users/findCommonData", { id: Number(id) });
}

/**
 * 分页获取指定用户的帖子列表
 * @param authorId 作者ID
 * @param pageNum 页码（从1开始）
 * @param pageSize 每页数量
 * @returns Promise<ResultObject>
 */
export async function getUserPostsApi(
  authorId: number | string,
  pageNum: number = 1,
  pageSize: number = 10
) {
  return api.get(`/posts/author/${authorId}`, {
    params: { pageNum, pageSize },
  });
}

/**
 * 分页获取指定用户的资源列表
 * @param userId 用户ID
 * @param pageNum 页码
 * @param pageSize 每页数量
 * @returns Promise<ResultObject>
 */
export async function getUserResourcesApi(
  userId: number | string,
  pageNum: number = 1,
  pageSize: number = 10
) {
  return api.post("/resources/user/page", null, {
    params: {
      userId: Number(userId),
      pageNum,
      pageSize,
    },
  });
}

/**
 * 用户退出登录
 * @param id 用户id
 * @returns Promise<ResultObject>
 */
export async function logoutUserApi(id: number | string) {
  const params = new URLSearchParams();
  params.append("id", id.toString());
  return api.post("/users/logout", params, {
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
  });
}
